import immutable from '../../../lib/immutable';
import getIn from '../../../utils/getIn';
import loadFieldList from './loadFieldList';

/**
 * 获取详情组件的参数集
 * @param metaData
 * @param extra
 * @returns {{set_id: *, fields: string, conditions: {}}}
 */
export default function detailElementParam(metaData, extra = {}) {
    if (extra && extra.toJS) extra = extra.toJS();
    const set_id = getIn(extra, ['set_id']) || metaData.getIn(['fieldSet', 'id']);// 模型id,如果url上面有模型id，优先使用
    const field_needs = metaData.getIn(['fieldSet', 'needs']) ? metaData.getIn(['fieldSet', 'needs']).toJS() : []; // 必要字段
    const conditions = {};
    field_needs && field_needs.map((fid) => {
        if (fid && getIn(extra, [fid])) {
            conditions[fid] = getIn(extra, [fid]);
        }
    });
    const fields = loadFieldList(metaData).concat(field_needs).join(',');
    return immutable.fromJS({
        set_id,
        fields,
        conditions,
    });
}